package com.luoxin.apidriver.service;

import com.luoxin.apidriver.remote.ServiceDriverUserClient;
import com.luoxin.apidriver.remote.ServiceVerificationCodeClient;
import com.luoxin.internalcommon.constant.CommonStatusEnum;
import com.luoxin.internalcommon.constant.DriverCarConstants;
import com.luoxin.internalcommon.constant.IdentityConstant;
import com.luoxin.internalcommon.constant.TokenConstants;
import com.luoxin.internalcommon.dto.ResponseResult;
import com.luoxin.internalcommon.request.VerificationCodeDTO;
import com.luoxin.internalcommon.response.DriverUserExistsResponse;
import com.luoxin.internalcommon.response.NumberCodeResponse;
import com.luoxin.internalcommon.response.TokenResponse;
import com.luoxin.internalcommon.util.JwtUtils;
import com.luoxin.internalcommon.util.RedisPrefixUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class VerificationCodeService {
    @Autowired
    private ServiceDriverUserClient serviceDriverUserClient;
    @Autowired
    private ServiceVerificationCodeClient serviceVerificationCodeClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public ResponseResult checkAndSendVerificationCode(@RequestBody VerificationCodeDTO verificationCodeDTO){
        // 查询 server-driver-use查询该手机好的司机是否存在
        ResponseResult<DriverUserExistsResponse> driverUserExistsResponseResponseResult = serviceDriverUserClient.getUser(verificationCodeDTO.getDriverPhone());
        DriverUserExistsResponse data = driverUserExistsResponseResponseResult.getData();
        int ifExist = data.getIfExist();
        if (ifExist == DriverCarConstants.DRIVER_NOT_EXISTS){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS);
        }
        log.info("司机手机号：{},存在。",verificationCodeDTO.getDriverPhone());
        // 获取验证码
        ResponseResult<NumberCodeResponse> numberCodeResponseResponseResult = serviceVerificationCodeClient.getVerificationCode(6);
        NumberCodeResponse verificationCode = numberCodeResponseResponseResult.getData();
        log.info("验证码：{}",verificationCode.getNumberCode());
        // 调用第三方发送验证码
        // 存入redis
        String key = RedisPrefixUtils.generatorKeyByPhone(verificationCodeDTO.getDriverPhone(), IdentityConstant.DRIVER_IDENTITY);
        stringRedisTemplate.opsForValue().set(key, verificationCode.getNumberCode()+"", 2, TimeUnit.MINUTES);
        return ResponseResult.success();
    }

    public ResponseResult checkCode(String driverPhone, String verificationCode) {
        String key = RedisPrefixUtils.generatorKeyByPhone(driverPhone,IdentityConstant.DRIVER_IDENTITY);
        String codeRedis = stringRedisTemplate.opsForValue().get(key);
        if (codeRedis == null) {
            return ResponseResult.fail(CommonStatusEnum.VERIFY_CODE_ERROR.getCode(), CommonStatusEnum.VERIFY_CODE_ERROR.getValue());
        }
        if (!verificationCode.trim().equals(codeRedis)) {
            return ResponseResult.fail(CommonStatusEnum.VERIFY_CODE_ERROR.getCode(), CommonStatusEnum.VERIFY_CODE_ERROR.getValue());
        }

        // 颁发令牌
        String accessToken = JwtUtils.geneJsonWebToken(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.geneJsonWebToken(driverPhone, IdentityConstant.DRIVER_IDENTITY,TokenConstants.REFRESH_TOKEN_TYPE);
        log.info("accessToken:{}.refreshToken:{}",accessToken,refreshToken);

        // accessToken存储到redis中
        String accessTokenKey = RedisPrefixUtils.generatorTokenKey(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(accessTokenKey, accessToken,30,TimeUnit.DAYS);

        // refreshToken存储到redis中
        String refreshTokenKey = RedisPrefixUtils.generatorTokenKey(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(refreshTokenKey, refreshToken,31,TimeUnit.DAYS);
        // 响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }
}
